package com.lcredu.lcrliving.commodity.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.exceptions.ClientException;
import com.lcredu.common.util.R;
import com.lcredu.lcrliving.commodity.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @author kirito
 * @create 2023-09-30 14:10
 */
@RestController
@Slf4j
public class TestController {

    //编写方法-上传到阿里云指定的bucket【原生sdk】
//    @RequestMapping("/test")
//    public R testUpload() throws FileNotFoundException {
//        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
//        String endpoint = "oss-cn-guangzhou.aliyuncs.com";
//        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
//        String accessKeyId = "LTAI5tE3uGGJvhykkj3WsHFi";
//        String accessKeySecret = "3uAsZ0lnxopdGr8fG84bvBA5g1YOud";
//        // 填写Bucket名称，例如examplebucket。
//        String bucketName = "lcrliving-10000";
//        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
//        // 文件上传后的文件名
//        String objectName = "2023-09-30/Asuna五.png";
//        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
//        // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
//        // 这里指定你要上传的文件完整的路径
//        String filePath = "D:\\王五.png";
//
//        // 创建OSSClient实例。
//        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//
//        try {
//            InputStream inputStream = new FileInputStream(filePath);
//            // 创建PutObject请求。
//            ossClient.putObject(bucketName, objectName, inputStream);
//        } catch (OSSException oe) {
//            System.out.println("Caught an OSSException, which means your request made it to OSS, "
//                    + "but was rejected with an error response for some reason.");
//            System.out.println("Error Message:" + oe.getErrorMessage());
//            System.out.println("Error Code:" + oe.getErrorCode());
//            System.out.println("Request ID:" + oe.getRequestId());
//            System.out.println("Host ID:" + oe.getHostId());
//        } finally {
//            if (ossClient != null) {
//                ossClient.shutdown();
//            }
//        }
//        return null;
//    }

    //装配
    @Resource
    private OSSClient ossClient;

    //上传指定文件到bucket
    @RequestMapping("/test2")
    public R testUpload2() throws FileNotFoundException {
        InputStream inputStream = new FileInputStream("C:\\Users\\18442\\Pictures\\Camera Roll\\Asuna03.png");
        ossClient.putObject("lcrliving-10000", "Asuan学习.png", inputStream);
        ossClient.shutdown();
        return R.ok("上传ok");
    }

    //编写方法测试getCascadedCategoryId
    //传入的categoryId = 301 => [1,21,301]

    //装配CategoryService
    @Resource
    private CategoryService categoryService;

    @RequestMapping("/test3")
    public R testGetCascadedCategoryId() {
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(301l);

        return R.ok().put("data", cascadedCategoryId);
    }

}
